<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Http\Resources\Home\OrderResource\OrderCollection;
use App\Http\Resources\Home\OrderResource\OrderResource;
use App\Services\OrderService;
use Illuminate\Http\Request;

class OrderController extends Controller
{

    // 获取订单列表
    public function get_orders()
    {
        $order_service = new OrderService();
        $list = $order_service->getOrders()['data'];
        return $this->success(new OrderCollection($list));
    }


    public function create_order()
    {
        $order_service = new OrderService;
        $rs = $order_service->createOrder();
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }


    public function create_order_before()
    {
        $order_service = new OrderService;
        $rs = $order_service->createOrderBefore();
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/11--9:53
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:create_order_after
     * @effect:订单创建之后请求接口
     */
    public function create_order_after()
    {
        $order_service = new OrderService;
        $rs = $order_service->createOrderAfter();
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/11--9:54
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:pay
     * @effect:订单支付
     */
    public function pay()
    {
        $order_service = new OrderService();
        $rs = $order_service->payOrder();
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }

    // 修改订单状态 // 用户只能操作取消订单
    public function edit_order_status(Request $request)
    {
        $order_service = new OrderService();
        $rs = $order_service->editOrderStatus($request->id, $request->order_status, 'user');
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    // 获取订单详情
    public function get_order_info($id)
    {
        $order_service = new OrderService();
        $rs = $order_service->getOrderInfoById($id, 'user');
        return $rs['status'] ? $this->success(new OrderResource($rs['data']), $rs['msg']) : $this->error($rs['msg']);
    }
}
